package com.sunlong.jiangwei.web;


import com.sunlong.jiangwei.entity.JwStore;
import com.sunlong.jiangwei.entity.result.Result;
import com.sunlong.jiangwei.service.IJwStoreService;
import com.sunlong.jiangwei.util.ResultUtil;
import com.sunlong.jiangwei.vo.JwStoreVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import static com.sunlong.jiangwei.util.MinioUtils.getStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孙龙
 * @since 2021-02-19
 */
@RestController
@RequestMapping("/jiangwei/jw-store")
public class JwStoreController {
    @Autowired
    private IJwStoreService jwStoreService;

    /**
     * 查询所有店铺
     * @return
     */
    @GetMapping("/list")
    public Result<List<JwStoreVo>> findAll(){
        List<JwStoreVo> jwStoreList = jwStoreService.findAll();
        return ResultUtil.data(jwStoreList);
    }

    /**
     * 查询所有店铺
     * @return
     */
    @PostMapping("/list")
    public Result<JwStore> list(@RequestBody Map<String, Object> map){
        JwStore jwStore = jwStoreService.list(map);
        return ResultUtil.data(jwStore);
    }

    /**
     * 新增店铺
     * @param context
     * @return
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Map<String, Object> context) throws IOException {
        if (!context.containsKey("storeName")){
            return ResultUtil.error("sroreName参数不存在");
        }
        File file = ResourceUtils.getFile("classpath:default.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);
        context.put("inputStream",fileInputStream);
        context.put("mode",".jpg");
        Map<String, String> map = jwStoreService.add(context);
        if (map.containsKey("existError")){
            return ResultUtil.error(map.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    /**
     * 删除店铺
     * @param context
     * @return
     */
    @PostMapping("/delete")
    public Result<Void> delete(@RequestBody Map<String, Object> context){
        if (!context.containsKey("id")){
            return ResultUtil.error("id参数不存在");
        }
        Map<String, String> map = jwStoreService.delete(context);
        if (map.containsKey("existError")){
            return ResultUtil.error(map.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    /**
     * 修改店铺信息（名称，头像）
     * @param context
     * @return
     */
    @PostMapping("/modify")
    public Result<Void> modify(@RequestBody Map<String, Object> context) throws IOException {
        if (!context.containsKey("id")){
            return ResultUtil.error("id参数不存在");
        }
        if (!context.containsKey("file") ){
            return ResultUtil.error("file参数不存在");
        }
        if (!((String)context.get("file")).equals("")){
            ByteArrayInputStream stream = getStream(context);
            context.put("inputStream", stream);
        }
        Map<String, String> map = jwStoreService.modify(context);
        if (map.containsKey("existError")){
            return ResultUtil.error(map.get("errorMsg"));
        }
        return ResultUtil.success();
    }


}
